package com.wind.trademark.client.dao.impl;

import java.util.Map;

import org.apache.commons.lang.StringUtils;

public class CompanySql {

	public static String getGoolgeCtCompanyByCompanyNameSQL(Map<String, Object> params) {
		
		StringBuffer stringBuffer= new StringBuffer();
		stringBuffer.append("SELECT company.ID 'key',company.NAME 'value' FROM CT_COMPANY company WHERE 1=1 ");
		if(params.entrySet().size()==1&&StringUtils.isNotEmpty((String) params.get("key"))){
			stringBuffer.append("AND company.ID=:key   ");
		}
		if(StringUtils.isNotEmpty((String) params.get("queryParam"))){
			stringBuffer.append("AND company.NAME LIKE :queryParam ");

			
			params.put("queryParam", "%"+(String) params.get("queryParam")+"%");
		}

 		stringBuffer.append("ORDER BY company.ID");
		return stringBuffer.toString();
	}

	public static String queryCompanySQL(Map<String, Object> params) {
		StringBuffer stringBuffer= new StringBuffer();
		stringBuffer.append("SELECT ct_company.* FROM ct_company ct_company ");
		stringBuffer.append("WHERE 1=1 ");
		if(StringUtils.isNotEmpty((String)params.get("code"))){
			stringBuffer.append("AND LOWER(ct_company.CODE) LIKE :code ");
			params.put("code", "%"+params.get("code").toString().toLowerCase()+"%");
		}
		if(StringUtils.isNotEmpty((String)params.get("name"))){
			stringBuffer.append("AND LOWER(ct_company.NAME) LIKE :name ");
			params.put("name", "%"+params.get("name").toString().toLowerCase()+"%");
		}
		if(StringUtils.isNotEmpty((String)params.get("contact"))){
			stringBuffer.append("AND (LOWER(ct_company.FAX) LIKE :contact OR LOWER(ct_company.TEL) LIKE :contact) ");
			params.put("contact", "%"+params.get("contact").toString().toLowerCase()+"%");
		}
		if(StringUtils.isNotEmpty((String)params.get("type"))){
			stringBuffer.append("AND ct_company.TYPE =:type ");
		}
		stringBuffer.append("ORDER BY ct_company.CODE ");
		return stringBuffer.toString();
	}

	public static String queryContactorSQL(Map<String, Object> params) {
		StringBuffer stringBuffer= new StringBuffer();
		stringBuffer.append("SELECT ct_contactor.* FROM ct_contactor ct_contactor ");
		stringBuffer.append("WHERE 1=1 ");
		if(StringUtils.isNotEmpty((String)params.get("compId"))){
			stringBuffer.append("AND ct_contactor.COMP_ID=:compId ");
		}
		stringBuffer.append("ORDER BY ct_contactor.ID ");
		return stringBuffer.toString();
	}

	public static String deleteContactorsSQL() {
		StringBuffer stringBuffer= new StringBuffer();
		stringBuffer.append("DELETE ");
		stringBuffer.append("FROM ct_contactor  ");
		stringBuffer.append("WHERE id IN (:ids) ");
		return stringBuffer.toString();
	}

	public static String updateContactorUnDefaultSQL() {
		return "UPDATE ct_contactor SET ISDEFAULT = 'N' WHERE COMP_ID = :compId ";
	}

}
